package com.fish.yssb.ui.page.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fish.yssb.R;
import com.fish.yssb.db.bean.BaseInfoTable;
import com.fish.yssb.ui.page.dialog.PublicNoticeDialog;
import com.fish.yssb.ui.page.dialog.SetParaDialog;

import java.util.ArrayList;


/**
 * Created by WangChang on 2016/4/1.
 */
public class constParaAdapter extends RecyclerView.Adapter<constParaAdapter.BaseViewHolder> {
    public ArrayList<BaseInfoTable> dataList = new ArrayList<BaseInfoTable>();
    private Resources res;
    private Context m_context;
    public void replaceAll(ArrayList<BaseInfoTable> list) {
        dataList.clear();
        if (list != null && list.size() > 0) {
            dataList.addAll(list);
        }
        notifyDataSetChanged();
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        m_context =parent.getContext();
        return new realtimeYCViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.const_para_item, parent, false));
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {

        holder.setData(dataList.get(position));
    }

    private String strVal ="";

    @Override
    public int getItemCount() {
        return dataList != null ? dataList.size() : 0;
    }

    public class BaseViewHolder extends RecyclerView.ViewHolder {

        public BaseViewHolder(View itemView) {
            super(itemView);
        }

        void setData(BaseInfoTable data) {
        }
    }

    private class realtimeYCViewHolder extends BaseViewHolder {

        private PublicNoticeDialog noticeDialog;
        private SetParaDialog setParaDialog;
        private TextView tv_name;
        private TextView tv_addr;
        private TextView tv_val;
        private TextView tv_val_change;
        private TextView tv_unit;
        private LinearLayout ll_view;
        public realtimeYCViewHolder(View view) {
            super(view);
            tv_name = view.findViewById(R.id.tv_name);
            tv_addr = view.findViewById(R.id.tv_addr);
            tv_val = view.findViewById(R.id.tv_val);
            tv_val_change = view.findViewById(R.id.tv_val_change);
            tv_unit = view.findViewById(R.id.tv_unit);
            ll_view = view.findViewById(R.id.ll_view);
        }



        @Override
        void setData(final BaseInfoTable data) {
            if (data != null) {
                tv_name.setText(data.getName());
                tv_addr.setText(data.getConnAdr());
                String tmp = "";
                if(!"".equalsIgnoreCase(data.getDefualtVal()))
                {
                    tmp = data.getDefualtVal();
                }
                String tmp_change = "" +
                        "";
                if(!"".equalsIgnoreCase(data.getRealVal()))
                {
                    tmp_change = data.getRealVal();
                }
                tv_val_change.setText(tmp_change);
                tv_val.setText(tmp);
                tv_unit.setText(data.getUnit());


                ll_view.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        setParaDialog= new SetParaDialog((Activity) m_context, new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                String sendStatus = "";
                                if (view.getId() == R.id.btn_chancel_zq_u) {//暂存




                                    setParaDialog.dismiss();
                                } else {//生成
                                    strVal = setParaDialog.text_para.getText().toString();
                                    noticeDialog = new PublicNoticeDialog((Activity) m_context, new View.OnClickListener() {
                                        @Override
                                        public void onClick(View view) {
                                            String sendStatus = "";
                                            if (view.getId() == R.id.btn_chancel_notice) {//暂存

                                                noticeDialog.dismiss();
                                            } else {//生成
                                                data.setRealVal(strVal);
                                                notifyDataSetChanged();
                                                setParaDialog.dismiss();
                                                noticeDialog.dismiss();

                                            }
                                            noticeDialog.dismiss();

                                        }
                                    }, 1, "", "确认修改该参数？", "取消", "确定修改");
                                    noticeDialog.show();
                                }


                            }
                        },data.getName(),data.getDefualtVal(),data.getRealVal());
                        setParaDialog.show();



                    }
                });
            }


        }
    }


}
